#include <stdio.h>
#include <stdlib.h>

int totalFruit(int* fruits, int fruitsSize) {
    int left = 0;  // 滑动窗口的左边界
    int result = 0;  // 存储最大水果数量
    int fruitCount[10001] = {0};  // 假设水果种类编号从 0 到 10000

    // 右边界遍历整个数组
    for (int right = 0; right < fruitsSize; right++) {
        fruitCount[fruits[right]]++;  // 增加当前水果类型的数量

        // 如果窗口内的水果种类超过 2 种，收缩左边界
        while (fruitCount[fruits[right]] > 2) {
            fruitCount[fruits[left]]--;  // 减少左边界水果的数量
            left++;  // 左边界右移
        }

        // 计算当前窗口的大小并更新最大值
        result = result > (right - left + 1) ? result : (right - left + 1);
    }

    return result;  // 返回最大水果数
}

int main() {
    int fruits1[] = {1, 2, 1};
    int fruits2[] = {0, 1, 2, 2};
    int fruits3[] = {1, 2, 3, 2, 2};
    int fruits4[] = {3, 3, 3, 1, 2, 1, 1, 2, 3, 3, 4};
    
    printf("Result 1: %d\n", totalFruit(fruits1, 3));  // Output: 3
    printf("Result 2: %d\n", totalFruit(fruits2, 4));  // Output: 3
    printf("Result 3: %d\n", totalFruit(fruits3, 5));  // Output: 4
    printf("Result 4: %d\n", totalFruit(fruits4, 11)); // Output: 5
    printf("Size of long: %zu bytes\n", sizeof(int));

    
    return 0;
}